package mysql

import (
	"gorm.io/gorm"
	"video/videorpc/videorpc"
)

type Video struct {
	gorm.Model
	Title      string `gorm:"type:varchar(30);not null;comment:'视频标题'"`
	Category   string `gorm:"type:varchar(30);not null;comment:'视频分类'"`
	Link       string `gorm:"type:varchar(30);not null;comment:'视频地址'"`
	CoverLink  string `gorm:"type:varchar(30);not null;comment:'视频封面'"`
	Intro      string `gorm:"type:varchar(30);not null;comment:'视频简介'"`
	PlayNum    int    `gorm:"type:int;not null;comment:'播放量'"`
	LikeNum    int    `gorm:"type:int;not null;comment:'点赞数'"`
	CollectNum int    `gorm:"type:int;not null;comment:'收藏数'"`
	CommentNum int    `gorm:"type:int;not null;comment:'评论数'"`
	Status     int    `gorm:"type:tinyint;not null;comment:'视频状态'"`
	UserId     int    `gorm:"type:int;not null;comment:'用户id'"`
	TopicId    int    `gorm:"type:int;not null;comment:'话题id'"`
}

func (v *Video) VideoAdd(db *gorm.DB) error {
	return db.Debug().Create(v).Error
}

func (v *Video) FindVideoById(db *gorm.DB, id int64) error {
	return db.Debug().Where("id = ?", id).First(v).Error
}

func VideoList(db *gorm.DB, in *videorpc.VideoListRequest) []*videorpc.VideoList {
	var list []*videorpc.VideoList

	if in.SortType == 0 {
		db.Debug().Model(&Video{}).
			Select("videos.*, videos.id, users.head_image, users.nick_name").
			Joins("left join users on users.id = videos.user_id").
			Scopes(Paginate(int(in.Page), int(in.Size))).
			Order("videos.play_num desc").
			Find(&list)
	}

	if in.SortType == 1 {
		db.Debug().Model(&Video{}).
			Select("videos.*, videos.id, users.head_image, users.nick_name").
			Joins("left join users on users.id = videos.user_id").
			Scopes(Paginate(int(in.Page), int(in.Size))).
			Order("videos.created_at desc").
			Find(&list)
	}

	return list
}

func VideoShow(db *gorm.DB, in *videorpc.VideoShowRequest) []*videorpc.VideoShow {
	var show []*videorpc.VideoShow

	tx := db.Debug().Model(&Video{}).
		Select("videos.*, videos.id, users.head_image, users.nick_name").
		Joins("left join users on users.id = videos.user_id").
		Order("videos.play_num desc").
		Limit(3).
		Find(&show)

	if in.TopicId != 0 {
		tx = tx.Where("videos.topic_id = ?", in.TopicId)
	}

	if in.UserId != 0 {
		tx = tx.Where("videos.user_id = ?", in.UserId)
	}

	return show
}

func FirstVideoShow(db *gorm.DB, videoId int64) videorpc.CommentListResponse {
	var show videorpc.CommentListResponse

	db.Debug().Model(&Video{}).
		Select("videos.*, videos.id, users.head_image, users.nick_name").
		Joins("left join users on users.id = videos.user_id").
		Where("videos.id = ?", videoId).
		First(&show)

	return show
}

func CollectList(db *gorm.DB, in *videorpc.CollectListRequest) []*videorpc.CollectList {
	var list []*videorpc.CollectList

	db.Debug().Model(&Video{}).
		Select("videos.*, videos.id, users.head_image, users.nick_name").
		Joins("left join users on users.id = videos.user_id").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Order("videos.play_num desc").
		Where("users.id = ?", in.UserId).
		Find(&list)

	return list
}
